package com.koron.operation.classes.form.controller;


import com.alibaba.fastjson.JSON;
import com.koron.bean.base.Response;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.bean.workflow.WorkflowController;
import com.koron.common.core.business.workflow.templates.WorkFlowHandler;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.operation.classes.form.bean.constants.ClassesFormUtil;
import com.koron.operation.classes.form.bean.dto.ClassesFormChangeDTO;
import com.koron.operation.classes.form.bean.po.ClassesFormChange;
import com.koron.operation.classes.form.service.ClassesFormChangeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 班别调整表(ClassesFormChange)表控制层
 *
 * @author jzp
 * @since 2023-05-22 09:48:16
 */
@Api(tags = "班别调整表接口")
@RestController
@RequestMapping("classesFormChange")
public class ClassesFormChangeController implements WorkflowController {
    /**
     * 服务对象
     */
    @Autowired
    private ClassesFormChangeService classesFormChangeService;

    @Autowired
    private WorkFlowHandler workFlowHandler;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    @ApiOperation(value = "根据id查询详情")
    @GetMapping("detail")
    public Response<ClassesFormChangeDTO> selectDetail(@RequestParam String id) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), classesFormChangeService, "getDtoById", ClassesFormChangeDTO.class, id));
    }

    @ApiOperation(value = "根据shiftId查询详情")
    @GetMapping("detailByShiftId")
    public Response<ClassesFormChangeDTO> selectDetailByShiftId(@RequestParam String shiftId) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), classesFormChangeService, "getDtoByShiftId", ClassesFormChangeDTO.class, shiftId));
    }

    @ApiOperation(value = "根据查询条件查询所有详情")
    @GetMapping("listDetail")
    public Response<ClassesFormChangeDTO> listDetail(ClassesFormChange classesFormChange) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), classesFormChangeService, "getDtoByShiftId", ClassesFormChangeDTO.class, classesFormChange));
    }

    @ApiOperation(value = "保存或更新数据")
    @PostMapping
    public Response<ClassesFormChangeDTO> save(@RequestBody ClassesFormChangeDTO dto) {
        ADOConnection.runTask(getCurrDs(), classesFormChangeService, "saveDto", Boolean.class, dto);
        return Response.ok(dto);
    }

    @ApiOperation(value = "删除数据")
    @DeleteMapping
    public Response<String> delete(@RequestParam String id) {
        ADOConnection.runTask(getCurrDs(), classesFormChangeService, "deleteById", Boolean.class, id);
        workFlowHandler.delete(id);
        return Response.ok();
    }

    @Override
    public Response<Object> updateState(@RequestBody RedirectQuery redirectQuery) {
        ClassesFormChangeDTO classesFormChangeDTO = JSON.parseObject(redirectQuery.getFormVariables(), ClassesFormChangeDTO.class);
        if (redirectQuery.getOperation() == 16) {
            // 如果是废弃，状态改为草稿，并删除流程
            ADOConnection.runTask(getCurrDs(), classesFormChangeService, "updateStatus", Boolean.class, redirectQuery.getDataId(), ClassesFormUtil.Status.DRAFT);
            workFlowHandler.delete(redirectQuery.getDataId());
        } else {
            ADOConnection.runTask(getCurrDs(), classesFormChangeService, "submit", Boolean.class, classesFormChangeDTO, redirectQuery.getCurNodeCode());
        }
        return Response.success();
    }

}
